package com.example.wheresmystuff2;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

import com.example.wheresmystuff2.Gallery;
import com.example.wheresmystuff2.ItemPage;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

/**
 * Class that represents the Gallery. It handles the back-end to display submitted items in a list
 */
public class Gallery extends Activity {
	Database db;
	private static Map<String, ArrayList<Object>> items;// =new HashMap<String, ArrayList<Object> >();
	private static Map<String, ArrayList<Object>> filteredItems;
	private static boolean filtered=false;
	
	private ListView mainListView;
	private ArrayAdapter<String> lostListAdapter;
	private ArrayAdapter<String> foundListAdapter;
	
	/**
	 * Method that is call when created. Creates required instance variables to display a list of items.
	 * @param savedInstanceState the state that was last saved
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		db = new Database(this);
		db.open();
		items = db.getItemTableAsHashMap();
		
		filteredItems=FilterPage.filterItems(items,"Any", "Any", new Date(0,1,1));
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallery);
		
		// Find the ListView resource.   
	    mainListView = (ListView) findViewById( R.id.itemListView);  
	  
	    // Create and fill list of items
	    ArrayList<String> itemList = new ArrayList<String>();
	    ArrayList<String> lostItemList = new ArrayList<String>();
	    ArrayList<String> foundItemList = new ArrayList<String>();
	    
	    for(String s: filteredItems.keySet()){
	    	//add to overall list
	    	itemList.add(s);
	    	Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
	    	//add to lost list
	    	if (filteredItems.get(s).get(4).equals("Lost")) 
				lostItemList.add(s);
	    	//add to found list
			else if (filteredItems.get(s).get(4).equals("Found"))
				foundItemList.add(s);
	    }
	    
	    // Create ArrayAdapters using the item list
	    lostListAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, lostItemList);
	    foundListAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, foundItemList);
	      
	    // Set the ArrayAdapter as the ListView's adapter 
	    mainListView.setAdapter(lostListAdapter);     
	    
	    mainListView.setOnItemClickListener( new OnItemClickListener() {

	    	/**
	    	 * On item click listener for list view
	    	 * 
	    	 * @param arg0
	    	 * @param arg2 position in the list
	    	 * @param arg3
	    	 */
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				String item=mainListView.getItemAtPosition(arg2).toString();
				Toast.makeText(getApplicationContext(), "You chose: "+item, Toast.LENGTH_SHORT).show();
				
				
		    	Intent myIntent = new Intent(Gallery.this, ItemPage.class);
		    	//pass string "item" as an extra into new activity
		    	myIntent.putExtra("itemName", item);
		    	//start intent
		    	Gallery.this.startActivity(myIntent);
				
			}
	    	
	    });
	    
	    //Visuals for the activity
	    final Button add_item_button = (Button) findViewById(R.id.add_item_from_gallery);
	    final Button filter_button = (Button) findViewById(R.id.filter_from_gallery);
	    final Button logout_button = (Button) findViewById(R.id.gallery_logout);
	    final Button search_button = (Button) findViewById(R.id.gallery_search);
	    
	    final Button lost_button = (Button) findViewById(R.id.gallery_lost);
	    final Button found_button = (Button) findViewById(R.id.gallery_found);
	    
	    //lost button starts out pressed
	    lost_button.setEnabled(false);
  	  	lost_button.setClickable(false);
  	  	
	    /**
	     * Add Item button onClick
	     */
	    add_item_button.setOnClickListener(new OnClickListener()
	    {
	    	/**
	    	 * This onClick brings you to the Add Item activity so you can enter your item details
	    	 */
	    	public void onClick(View v){
	    		Intent myIntent = new Intent(Gallery.this, AddItem.class);
	    		Gallery.this.startActivity(myIntent);
	    	}
	    });
	    
	    /**
	     * Filter button onClick
	     */
	    filter_button.setOnClickListener(new OnClickListener()
	    {
	    	/**
	    	 * This onClick method brings you to the filter page so you can filter the items by certain parameters
	    	 */
	    	public void onClick(View v){
	    		Intent myIntent = new Intent(Gallery.this, FilterPage.class);
	    		Gallery.this.startActivity(myIntent);
	    	}
	    });
	    
	    /**
	     * Logout button onClick
	     */
	    logout_button.setOnClickListener(new OnClickListener()
	    {
	    	/**
	    	 * This onClick method logs out the user... (more so, brings you to login page)
	    	 */
	    	public void onClick(View v){
	    		Intent myIntent = new Intent(Gallery.this, LogIn.class);
	    		Gallery.this.startActivity(myIntent);
	    	}
	    });
	    
	    /**
	     * Search button onClick
	     */
	    search_button.setOnClickListener(new OnClickListener()
	    {
	    	/**
	    	 * This onClick method brings you to the Search activity that allows you to search for a keyword.
	    	 */
	    	public void onClick(View v){
	    		Intent myIntent = new Intent(Gallery.this, Search.class);
	    		Gallery.this.startActivity(myIntent);
	    	}
	    });
	    
	    /**
	     * Lost button onClick
	     */
	    lost_button.setOnClickListener(new OnClickListener()
	    {
	    	public void onClick(View v)
	    	{
	    		found_button.setEnabled(true);
	    		found_button.setClickable(true);
	    		lost_button.setEnabled(false);
	    		lost_button.setClickable(false);
	    		//make show lost items
	    		mainListView.setAdapter(lostListAdapter);
	    	}
	    });
	    
	    /**
	     * Found button onClick
	     */
	    found_button.setOnClickListener(new OnClickListener()
	    {
	    	public void onClick(View v){
	    		lost_button.setEnabled(true);
	    		lost_button.setClickable(true);
	    		found_button.setEnabled(false);
	    		found_button.setClickable(false);
	    		//make show found items
	    		mainListView.setAdapter(foundListAdapter);
	    	}
	    });	    
	}
	
	/**
	 * Method that creates options menu
	 * @param menu the menu used
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.gallery, menu);
		return true;
	}

	/**
	 * Getter method for the list of items
	 */
	public static Map<String, ArrayList<Object>> getItems() {
		return items;
	}
	
	public static Map<String, ArrayList<Object>> getFilteredItems() {
		return filteredItems;
	}

	public static void setFilteredItems(Map<String, ArrayList<Object>> filteredItems) {
		Gallery.filteredItems = filteredItems;
	}
	
	public static void setFiltered(Boolean filtered){
		Gallery.filtered=filtered;
	}	
}
